package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.RegexUtils;

public class Hi0766BbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://hi0766.com/read.php[?]tid=[0-9]*"
		};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String commnetNum = null;
		String clickNum = null;

		Document doc = Jsoup.parse(htmltxt);

		title = doc.select("div.h1 h1").text();
		if(StringUtils.isBlank(title)){
			title = doc.select("h1#subject_tpc").text();
		}
		date = DateUtils.matchDate(doc.select("div.cate_r_tips span").text());
		if(date==null){
			date = DateUtils.matchDate(doc.select("span:contains(发表于:)").text());
		}
		content = HtmlCleaner.getContentHtml(url, doc.select("div.tpc_content"));

		/**
		 * 初始化、截取 、设置作者信息*******************************************
		 *
		 * */
		String author = null;
		Elements authorElements = doc.select("div.cate_r_tips span");
		if(authorElements!=null&&authorElements.size()>0){
			String authorStr = authorElements.get(0).text();
			author = StringUtils.substringAfter(authorStr, "作者：");
		}

		bbs.setAuthor(author);
		/**
		 *                        	 **************************end *************************
		 *
		 **/
		bbs.setTitle(title);
		bbs.setContent(content);
		bbs.setDate(date);
		/**
		 * 解析用于更新的地址
		 */
		bbs.setUpdateUrl(url);
		bbs.setClickNum(NumberUtils.parseInt(clickNum));
		bbs.setCommentNum(NumberUtils.parseInt(commnetNum));
		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if (StringUtils.isNotBlank(updateUrl)){
				UrlMeta urlMeta = CrawlHTML.responseToURL(updateUrl);
				return parserHtml(urlMeta);
			}
			return null;
		}
		return null;
	}

	public static void main(String[] args) {
		Hi0766BbsAnalyse a = new Hi0766BbsAnalyse();

		String url = "http://hi0766.com/read.php?tid=43152";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		if (!a.isDetailPage(url)) {
			System.out.println("不匹配规则");
		} else {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
//			System.out.println(a.Update(parserHtml));
		}

	}
	
}
